package com.csd.view;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TimePicker;

import com.csd.activity.R;
import com.csd.wheel.widget.WheelView;
import com.csd.wheel.widget.adapters.AbstractWheelTextAdapter;
import com.csd.wheel.widget.adapters.NumericWheelAdapter;


public class TimePopupwindow extends PopupWindow {

	private View conentView;
	private TextView text,tv_taketime,tv_returntime;
	private LinearLayout wheel_taketime,wheel_returntime;

	public TimePopupwindow(final Activity context,final TextView text) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		conentView = inflater.inflate(R.layout.pop_time, null);
		tv_taketime = (TextView) conentView.findViewById(R.id.tv_taketime);
		wheel_taketime = (LinearLayout) conentView.findViewById(R.id.wheel_taketime);
		int h = context.getWindowManager().getDefaultDisplay().getHeight();
		int w = context.getWindowManager().getDefaultDisplay().getWidth();
		// 设置SelectPicPopupWindow的View
		this.setContentView(conentView);
		// 设置SelectPicPopupWindow弹出窗体的宽
		this.setWidth(w - 32);
		// 设置SelectPicPopupWindow弹出窗体的高
		this.setHeight(LayoutParams.WRAP_CONTENT);
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		this.setOutsideTouchable(true);
		// 刷新状态
		this.update();
		
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(0000000000);
		// 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
		this.setBackgroundDrawable(dw);
		// mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
		// 设置SelectPicPopupWindow弹出窗体动画效果
		this.setAnimationStyle(R.style.AnimationPreview);
		Button bt_cancle = (Button) conentView
				.findViewById(R.id.cancle);
		Button bt_sure = (Button) conentView
				.findViewById(R.id.sure);
		bt_cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				TimePopupwindow.this.dismiss();
			}
		});
		bt_sure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				TimePopupwindow.this.dismiss();
				
			}
		});

		  final WheelView hours = (WheelView) conentView.findViewById(R.id.hour);
	        NumericWheelAdapter hourAdapter = new NumericWheelAdapter(context, 0, 23);
	        hourAdapter.setItemResource(R.layout.wheel_text_item);
	        hourAdapter.setItemTextResource(R.id.text);
	        hours.setViewAdapter(hourAdapter);
	    
	        final WheelView mins = (WheelView) conentView.findViewById(R.id.mins);
	        NumericWheelAdapter minAdapter = new NumericWheelAdapter(context, 0, 59, "%02d");
	        minAdapter.setItemResource(R.layout.wheel_text_item);
	        minAdapter.setItemTextResource(R.id.text);
	        mins.setViewAdapter(minAdapter);
	        mins.setCyclic(true);
	        
	        // set current time
	        Calendar calendar = Calendar.getInstance(Locale.CHINA);
	        hours.setCurrentItem(calendar.get(Calendar.HOUR_OF_DAY));
	        mins.setCurrentItem(calendar.get(Calendar.MINUTE));
	        
	        final WheelView day = (WheelView) conentView.findViewById(R.id.day);
	        day.setViewAdapter(new DayArrayAdapter(context, calendar)); 
			 
		        
	}

	public void showPopupWindow(View parent) {
		if (!this.isShowing()) {
			// 以下拉方式显示popupwindow
			this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 18);
		} else {
			this.dismiss();
		}
	}
	
	 /**
     * Day adapter
     *
     */
    private class DayArrayAdapter extends AbstractWheelTextAdapter {
        // Count of days to be shown
        private final int daysCount = 1;
        
        // Calendar
        Calendar calendar;
        
        /**
         * Constructor
         */
        protected DayArrayAdapter(Context context, Calendar calendar) {
            super(context, R.layout.time2_day, NO_RESOURCE);
            this.calendar = calendar;
            
            setItemTextResource(R.id.time2_monthday);
        }

        @Override
		public View getItem(int index, View cachedView, ViewGroup parent) {
            int day = -daysCount/2 + index;
            Calendar newCalendar = (Calendar) calendar.clone();
            newCalendar.roll(Calendar.DAY_OF_YEAR, day);
            
            View view = super.getItem(index, cachedView, parent);

            TextView monthday = (TextView) view.findViewById(R.id.time2_monthday);
            if (day == 0) {
                monthday.setText("今天");
                monthday.setTextColor(0xFF111111);
            } else if(day == 1){
            	monthday.setText("明天");
            	monthday.setTextColor(0xFF111111);
            }
            else {
                DateFormat format = new SimpleDateFormat("MMM d");
                monthday.setText(format.format(newCalendar.getTime()));
                monthday.setTextColor(0xFF111111);
            }

            return view;
        }
        
        @Override
        public int getItemsCount() {
            return daysCount + 1;
        }
        
        @Override
        protected CharSequence getItemText(int index) {
            return "";
        }
    }
}
